package unify.data;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable(tableName = "shows")
public class Show implements Comparable<Show> {
	@SuppressWarnings("unused")
	@DatabaseField(generatedId = true)
	private int databaseId;
	@ForeignCollectionField(eager = true)
	public Collection<Season> seasons = new ArrayList<Season>();
	@DatabaseField
	private String title;
	@DatabaseField
	private int curSeason;
	@DatabaseField
	private int LastEpisode;
	@DatabaseField
	private String altTitles;
	private String altTitleDelimiter = "<br>";

	/**
	 * Ormlite no-arg constructor
	 */
	public Show() {
		super();
	}
	public Show(String title) {
		super();
		this.title = title;
		this.altTitles = "";
	}

	/**
	 * Adds a season to the list of seasons for this show.
	 * No longer sorted since switched to collections, will this be an issue?
	 * Should only ever be called if the show does not already contain a season with that number.
	 * 
	 * @param seasonInt The number of the season to be added.
	 * @return season The newly added season.
	 */
	public Season addSeason(int seasonInt) {
		Season season = new Season(seasonInt, this);
		this.seasons.add(season);
		return season;
	}

	public Season getSeason() {
		for(Season season : this.seasons) {
			return season;
		}
		return null;
	}

	public Season findSeason(int num) {
		for(Season season : this.seasons) {
			if(season.getNumber()==num) {
				return season;
			}
		}
		return null;
	}

	public void removeSeason(Season season) {
		for (Iterator<Season> it = this.seasons.iterator(); it.hasNext();) {
			if (it.next().getNumber()==season.getNumber()) {
				it.remove();
			}
		}
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public int getCurSeason() {
		return curSeason;
	}

	public void setCurSeason(int curSeason) {
		this.curSeason = curSeason;
	}

	public int getLastEpisode() {
		return LastEpisode;
	}

	public void setLastEpisode(int LastEpisode) {
		this.LastEpisode = LastEpisode;
	}

	public void addAltTitle(String title) {
		if(!isThisMyTitle(title)) {
			this.altTitles = this.altTitles + title + altTitleDelimiter;
		}
	}

	public boolean isThisMyTitle(String myTitle) {
		if(myTitle.equalsIgnoreCase(this.title)) {
			return true;
		}
		String[] altTitles = this.altTitles.split(altTitleDelimiter);
		for(int i=0;i<altTitles.length;i++) {
			if(myTitle.equalsIgnoreCase(altTitles[i])) {
				return true;
			}
		}
		return false;
	}

	public String getAltTitles() {
		if(this.altTitles.isEmpty()) {
			return "No alternate titles for " + this.title + ".";
		}
		return this.altTitles;
	}

	public void removeAltTitle(String title) {
		String[] altTitles = this.altTitles.split(altTitleDelimiter);
		this.altTitles = "";
		for(int i=0;i<altTitles.length;i++) {
			if(!title.equals(altTitles[i])) {
				addAltTitle(altTitles[i]);
			}
		}
	}
	
	@Override
	public int compareTo(Show show) {
		return this.title.compareTo(show.getTitle());
	}
}
